package cn.xiaopengstack.domain.order.service;

import cn.xiaopengstack.domain.order.model.aggregate.CreateOrderAggregate;
import cn.xiaopengstack.domain.order.model.entity.OrderEntity;
import cn.xiaopengstack.domain.order.model.entity.PayOrderEntity;
import cn.xiaopengstack.domain.order.model.entity.ProductEntity;
import cn.xiaopengstack.domain.order.model.entity.ShopCarEntity;
import cn.xiaopengstack.domain.order.model.valobj.PayOrderStatusEnumVO;
import cn.xiaopengstack.domain.order.repository.IOrderRepository;
import cn.xiaopengstack.domain.product.adapter.IProductPort;
import com.alipay.api.AlipayApiException;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * @author jiangyangang
 */
@Slf4j
public abstract class AbsOrderService implements IOrderService{

    protected final IOrderRepository orderRepository;

    protected final IProductPort productPort;

    public AbsOrderService(IOrderRepository orderRepository, IProductPort productPort) {
        this.orderRepository = orderRepository;
        this.productPort = productPort;
    }

    @Override
    public PayOrderEntity createOrder(ShopCarEntity shopCarEntity) throws AlipayApiException {
        // redis锁

        // 判断订单状态
        OrderEntity orderEntity = orderRepository.queryUnPayOrder(shopCarEntity);
        if (Objects.nonNull(orderEntity) && orderEntity.getOrderStatusVO() == PayOrderStatusEnumVO.pay_wait) {
            log.info("【创建订单】，当前订单, 未支付。orderId={}, userId={}, productId={}", orderEntity.getOrderId(), shopCarEntity.getUserId(), shopCarEntity.getProductId());
            return PayOrderEntity.builder()
                    .orderStatus(orderEntity.getOrderStatusVO())
                    .payUrl(orderEntity.getPayUrl())
                    .orderId(orderEntity.getOrderId())
                    .build();
        } else if (Objects.nonNull(orderEntity) && orderEntity.getOrderStatusVO() == PayOrderStatusEnumVO.create) {
            return PayOrderEntity.builder()
                    .orderId(orderEntity.getOrderId())
                    .orderStatus(orderEntity.getOrderStatusVO())
                    .build();
        }

        // 查询产品信息
        ProductEntity productEntity = productPort.queryProductByProductId(shopCarEntity.getProductId());

        orderEntity = CreateOrderAggregate.buildInitOrderEntity(productEntity.getProductId(), productEntity.getProductName());

        CreateOrderAggregate createOrderAggregate = CreateOrderAggregate.builder()
                .productEntity(productEntity)
                .shopCarEntity(shopCarEntity)
                .orderEntity(orderEntity)
                .build();

        doSaveOrder(createOrderAggregate);

        return PayOrderEntity.builder()
                .orderStatus(PayOrderStatusEnumVO.create)
                .orderId(orderEntity.getOrderId())
                .totalAmount(productEntity.getPrice())
                .productName(productEntity.getProductName())
                .productId(productEntity.getProductId())
                .userId(shopCarEntity.getUserId())
                .build();
    }

    protected abstract void doSaveOrder(CreateOrderAggregate createOrderAggregate);

}
